package cn.mesmile.h2.controller;

import cn.mesmile.h2.common.R;
import cn.mesmile.h2.entity.Student;
import cn.mesmile.h2.service.StudentService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zb
 * @date 2021/11/22 10:55
 * @Description
 */
@RequestMapping("/api/v1/student")
@RestController
public class StudentController {

    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    @GetMapping("/list")
    public R get(){
        List<Student> studentList = studentService.listStudent();
        return R.data(studentList);
    }

    @PostMapping("/add")
    public R add(@RequestBody Student student){
        boolean save = studentService.save(student);
        return R.data(student);
    }

    @PostMapping("/update")
    public R update(@RequestBody Student student){
        Student result = studentService.updateStudent(student);
        return R.data(result);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable("id")Long id){
        boolean b = studentService.removeById(id);
        return R.status(b);
    }

}
